<script type="text/javascript">
function saveDraft ()
{
	//alert ( $('form').serialize() );
	$.ajax({
		type: "POST",
		url:'<? echo WEB_ROOT; ?>compose/saveDraft/',
		data: $('form').serialize(),
		success: function(data){
			if ( data == '-1' )
			{
				alert ( "Une erreur est survenue lors de la sauvegarde de l'email dans les brouillons, veuillez réessayer plus tard." );
			}
			else
			{
				<?php /*alert('res: ' + data);*/ ?>
				$('#uid').attr('value', data);
				<?php /*alert('val: ' + $('#uid').attr('value'));*/ ?>
				alert ( "L'email a bien été sauvegardé dans les brouillons." );
			}
		}
	});
}

function checkForm ()
{
	<?php /*alert ( 'email[to]:#' + $('#email[to]').attr('value') + '#' );
	alert ( 'email[subject]:#' + $('#email[subject]').attr('value') + '#' );
	if ( $('#email[to]').attr('value') == '' || $('#email[to]').attr('value') == undefined || $('#email[subject]').attr('value') == '' || $('#email[subject]').attr('value') == undefined )
	*/ ?>
	var e = document.getElementById ( 'email[to]' );
	var e2 = document.getElementById ( 'email[subject]' );
	if ( e.value.length == 0 || e2.value.length == 0 )
	{
		alert ( 'Votre email doit comporter au moins un destinataire et un sujet.' );
	}
	else
	{
		$('#composeform').submit ();
		$('#composeform').attr ( 'action', '#' );
	}
}
</script>

<div data-role="header" data-nobackbtn="true">
	<?php $path = empty($_SERVER['HTTP_REFERER']) ? WEB_ROOT."webmail/menu" : $_SERVER['HTTP_REFERER']; ?>
	<a data-icon="arrow-l" rel="external" href="<?php echo $path; ?>">Retour</a>
	<h1>Composer</h1>
	<a class="ui-btn-right" onClick="checkForm();">Envoyer</a>
</div><!-- /header -->

<?php
if ( isset($_SESSION['email']) && is_array($_SESSION['email']) )
	extract($_SESSION['email']);
?>

<div data-role="content">
	<?php
		/*echo '<p>session:#';
		print_r ( $_SESSION['email'] );
		echo '#;session_to:#' . $to . '#'; 
		echo '</p>';*/
	?>
	<form method="post" id="composeform" action="<?php echo WEB_ROOT ?>compose/sendMessage">
		<input type="hidden" name="email[uid]" id="uid" value="<?php if ( isset ($this->vars['uid']) && !empty($this->vars['uid']) ) echo $this->vars['uid']; else if ( isset ($uid) && !empty($uid) ) echo $uid; else echo '-1'; ?>" />
		<label id="labelTo" for="email[to]"></label>
		<input type="email" name="email[to]" id="email[to]" placeholder='&Agrave;' value="<?php if ( isset ($this->vars['to']) && !empty($this->vars['to']) ) echo $this->vars['to']; else if ( isset ($to) && !empty($to) ) echo $to; ?>" />
		<div data-role="collapsible" data-collapsed="<?php echo ( (isset($this->vars['cc']) && !empty($this->vars['cc'])) || (isset($this->vars['bcc']) && !empty($this->vars['bcc'])) || (isset($cc) && !empty($cc)) || (isset($bcc) && !empty($bcc)) ) ? 'false' : 'true'; ?>">
			<h3>Cc/Bcc</h3>
			<label id="labelCc" for="email[cc]"></label>
			<input type="email" name="email[cc]" id="email[cc]" placeholder='Cc' value="<?php if ( isset ($this->vars['cc']) && !empty($this->vars['cc']) ) echo $this->vars['cc']; else if ( isset ($cc) && !empty($cc) ) echo $cc; ?>" /><br />
			<label id="labelBcc" for="email[bcc]"></label>
			<input type="email" name="email[bcc]" id="email[bcc]" placeholder='Bcc' value="<?php if ( isset ($this->vars['bcc']) && !empty($this->vars['bcc']) ) echo $this->vars['bcc']; else if ( isset ($bcc) && !empty($bcc) ) echo $bcc; else ?>" /><br />
		</div>
		<label id="labelSubject" for="email[subject]"></label>
		<input type="text" name="email[subject]" id="email[subject]" placeholder='Objet' value="<?php if ( isset ($this->vars['subject']) && !empty($this->vars['subject']) ) echo $this->vars['subject']; else if ( isset ($subject) && !empty($subject) ) echo $subject; ?>" /><br />
		<label id="labelContent" for="email[content]"></label>
		<textarea name="email[content]" id="email[content]" placeholder='Message' style="height: 150px;"><?php if ( isset ($this->vars['content']) && !empty($this->vars['content']) ) echo $this->vars['content']; else if ( isset ($content) && !empty($content) ) echo $content; ?></textarea><br />
		<?php /*<input type="submit" value="Envoyer" onSubmit="return checkForm ()" />*/ ?>
		<a data-role="button" onClick="checkForm ();">Envoyer</a>
	</form>
</div>

<div data-role="footer">
	<div style="float: left; margin-left: 20%" class="save">
			<a data-icon="check" id="saveDraft" data-role="button" onClick="saveDraft();">Sauver</a>
	</div>
	<div style="float: right; margin-right: 20%">
		<a id="deco" onClick="if(confirm('Voulez-vous vraiment vous déconnecter ?')){this.setAttribute('href', '<?php echo WEB_ROOT; ?>member/logout');}" rel="external">
			<div style="width: 32px; height: 32px; line-height: 32px; text-align: center;">
				<img src="<?php echo WEB_ROOT; ?>assets/img/logout.png" style="vertical-align: middle;" alt="Logout" />
			</div>
		</a>
	</div>
</div><!-- /footer -->


<script type="text/javascript">
$(document).ready(function() {
	jQuery.extend(jQuery.mobile,{
		ajaxFormsEnabled : false
	});
});
</script>

<script type="text/javascript">
if ( !supports_input_placeholder() ) {
	var e;
	
	e = $("#labelTo");
	if ( e != null ) {
		e.html("&Agrave; :<br/>");
	}
	
	e = $("#labelCc");
	if ( e != null ) {
		e.html("Cc :<br/>");
	}
	
	e = $("#labelBcc");
	if ( e != null ) {
		e.html("Bcc :<br/>");
	}
	
	e = $("#labelSubject");
	if ( e != null ) {
		e.html("Objet :<br/>");
	}
	
	e = $("#labelContent");
	if ( e != null ) {
		e.html("Message :<br/>");
	}
}
</script>
